#include "StdAfx.h"
#include "Canvas.h"
#include "MGisDoc.h"
#include "MapLayer.h"
#include "drawer.h"
#include "GISApplication.h"
#include "MapLayerMgr.h"
#include "drawer.h"
#include "DeviceContext.h"

CCanvas::CCanvas(CGISApplication* pGISApplication)
{
    m_pGISApplication = pGISApplication;
    m_pMap2ClientTrans = new CMap2ClientTransform();
	m_pDrawer = new CDrawer();
	m_pDrawer->setMap2ClientTrans(m_pMap2ClientTrans);
	m_pDC = NULL;
}

CCanvas::~CCanvas(void)
{
	if (m_pMap2ClientTrans)
	{
		delete m_pMap2ClientTrans;
	}

	if (m_pDrawer)
	{
		delete m_pDrawer;
	}

	if (m_pDC)
	{
		delete m_pDC;
	}
}

void CCanvas::invalidate()
{
	m_pWnd->Invalidate();
}

void CCanvas::draw()
{
    CArray<CMapLayer*>* mapLayers = &(m_pGISApplication->getMapLayerMgr()->m_mapLayers);
	if (!mapLayers || mapLayers->GetSize() ==0)
	{
		return;
	}	
	
	for (int ii = 0; ii < mapLayers->GetCount(); ii++)
	{
		CMapLayer* maplayer = mapLayers->GetAt(ii);
		m_pDrawer->drawMapLayer(maplayer);
	}
}

void CCanvas::draw(CDeviceContext* pDC)
{
    CArray<CMapLayer*>* mapLayers = &(m_pGISApplication->getMapLayerMgr()->m_mapLayers);
    if (!mapLayers || mapLayers->GetSize() ==0)
    {
        return;
    }	

    CDrawer drawer;
    drawer.setDeviceContext(pDC);
    drawer.setMap2ClientTrans(m_pMap2ClientTrans);
    for (int ii = 0; ii < mapLayers->GetCount(); ii++)
    {
        CMapLayer* maplayer = mapLayers->GetAt(ii);
        drawer.drawMapLayer(maplayer);
    }
}

void CCanvas::setClientSize(CSize size)
{
    mClientSize = size;
    m_pMap2ClientTrans->setParameters(&mClientSize,&m_mapExtend);
}

void CCanvas::setMapExtend(CMapLayerEnvelope extend)
{    
    m_mapExtend = extend;
	m_pMap2ClientTrans->setParameters(&mClientSize,&extend);
}

void CCanvas::setMaxMapExtend(CMapLayerEnvelope extend)
{    
	m_mapMaxExtend = extend;
}

CMapLayerEnvelope CCanvas::getMapExtend()
{
	return m_mapExtend;
}

void CCanvas::setDeviceContext(CDeviceContext* pDC)
{
	//if (m_pDC)
	//{
	//	delete m_pDC;
	//}
	m_pDC = pDC;
	m_pDrawer->setDeviceContext(m_pDC);
}

void CCanvas::setWnd(CWnd* pWnd)
{
    m_pWnd = pWnd;
}
